package com.example.wuyou.juc;

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

public class HbThread extends  Thread{
    static double money=100;
    static int rs = 3;

    private static ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
            try {
                lock.lock();
                if (rs > 1) { // 检查是否还有剩余的票可以卖
                    rs--; // 原子地减少剩余票数
                    Random random = new Random();
                    // 生成 [1.0, 100.0) 范围内的小数
                    double decimal = 1 + (money - 1) * random.nextDouble();
                    money -= decimal;
                    System.out.println("窗口 " + getName() + " 卖出 @" + decimal);
                } else if (rs==1) {
                    System.out.println("窗口 " + getName() + " 卖出 @" + money);
                    rs --;
                    money = 0;
                } else {
                    System.out.println("窗口 " + getName() + " 没抢到");
                }
            } finally {
                lock.unlock(); // 确保锁被释放
            }
    }
}
